package com.guldan.jaina.manager;

import com.guldan.core.exception.BusinessException;
import com.guldan.core.manager.BaseManager;
import com.guldan.core.search.Pagination;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.news.NewsDTO;
import com.guldan.jaina.dto.news.NewsSearchDTO;
import com.guldan.jaina.utils.NewsDomainConvertUtils;
import com.guldan.mapper.db.entity.News;
import com.guldan.mapper.db.entity.NewsExample;
import com.guldan.mapper.db.mapper.NewsMapperExt;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Silas.
 * Date: 2017/1/24
 * Time: 11:37
 */
@Service
public class NewsManager extends BaseManager {

    @Autowired
    private NewsMapperExt newsMapperExt;

    public NewsDTO get(Integer newsId) {
        return NewsDomainConvertUtils.convert(newsMapperExt.selectByPrimaryKey(newsId));
    }

    public NewsDTO save(NewsDTO dto) throws BusinessException {
        News entity;
        if (dto.getNewsId() != null && dto.getNewsId() > 0) {
            entity = newsMapperExt.selectByPrimaryKey(dto.getNewsId());

            entity.setNewsId(dto.getNewsId());
            entity.setNewsType(dto.getNewsType());
            entity.setTitle(dto.getTitle());
            entity.setTags(dto.getTags());
            entity.setIsTop(dto.getTop());
            entity.setIsNotice(dto.getNotice());
            entity.setSort(dto.getSort());
            entity.setContent(dto.getContent());
            entity.setRemark(dto.getRemark());

            entity.setDisabled(dto.getDisabled());
            entity.setModifyTime(DateUtil.getCurrentTime());
            entity.setModifier(dto.getModifier());
            newsMapperExt.updateByPrimaryKeySelective(entity);
        } else {

            entity = new News();

            entity.setNewsType(dto.getNewsType());
            entity.setTitle(dto.getTitle());
            entity.setTags(dto.getTags());
            entity.setIsTop(dto.getTop());
            entity.setIsNotice(dto.getNotice());
            entity.setSort(dto.getSort());
            entity.setContent(dto.getContent());
            entity.setRemark(dto.getRemark());

            entity.setDisabled(true);
            entity.setCreator(dto.getModifier());
            entity.setCreateTime(DateUtil.getCurrentTime());
            entity.setIsDelete(false);
            int ret = newsMapperExt.insertSelective(entity);

//            int newsID=entity.getNewsId(); //可以直接拿到插入后自增的主键id

        }

        dto = NewsDomainConvertUtils.convert(entity);
        return dto;
    }

    //删除
    public Boolean delete(Integer id) {
        News entity = newsMapperExt.selectByPrimaryKey(id);
        entity.setIsDelete(true);
        entity.setModifyTime(DateUtil.getCurrentTime());
        return newsMapperExt.updateByPrimaryKeySelective(entity) > 0;
    }

    public Pagination<NewsDTO> list(NewsSearchDTO search) {
        Pagination<NewsDTO> result = new Pagination<NewsDTO>(search.getCurrentPage(), search.getPerPage());
        NewsExample example = buildSearchExample(search);
        example.setOrderByClause("is_top desc,sort asc,news_id desc");
        int total = newsMapperExt.countByExample(example);
        result.setTotalCount(total);
        if (total > 0) {
            List<NewsDTO> data = new ArrayList<NewsDTO>();
            List<News> list = newsMapperExt.selectByExampleWithPaging(example, result.getOffset(), result.getPageSize());
            for (News entity : list) {
                NewsDTO dto = NewsDomainConvertUtils.convert(entity);
                data.add(dto);
            }
            result.setList(data);
        }
        return result;
    }

    //build查询Example
    private NewsExample buildSearchExample(NewsSearchDTO search) {
        NewsExample example = new NewsExample();
        NewsExample.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(search.getTitle())) {
            criteria.andTitleLike("%" + search.getTitle() + "%");
        }
        if (search.getTop() != null) {
            criteria.andIsTopEqualTo(search.getTop());
        }

        if (search.getNewsId() != null && search.getNewsId() > 0) {
            criteria.andNewsIdEqualTo(search.getNewsId());
        }

        if (search.getIsNotice() != null) {
            criteria.andIsNoticeEqualTo(search.getIsNotice());
        }

        if (search.getNewsType() != null && search.getNewsType() > 0) {
            criteria.andNewsTypeEqualTo(search.getNewsType());
        }

        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        if (StringUtils.isNotBlank(search.getOrderByClause())) {
            example.setOrderByClause(search.getOrderByClause());
        } else {
            example.setOrderByClause("news_id desc");
        }
        return example;
    }

}
